<template>
  <div class="search">
    <el-autocomplete
      v-model="hosname"
      :fetch-suggestions="fetchData"
      clearable
      class="inline-input w-50"
      placeholder="请输入医院名称"
      @select="goDetail"
      :trigger-on-focus="false"
    />
    <el-button type="primary" size="default" :icon="Search"></el-button>
  </div>
</template>

<script setup lang="ts">
import { Search } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import { ref } from "vue";
import { reqHospitalInfo } from "@/api/home";
import type { HospistalInfo } from "@/api/home/type.ts";
// 创建路由器对象
let $router = useRouter();
// 收集搜索的关键字
let hosname = ref<string>("");

// 顶部组件的回调
const fetchData = async (keyword: string, cb: any) => {
  // 当用户输入完关键字函数会执行一次，发请求获取需要展示的数据即可
  let result: HospistalInfo = await reqHospitalInfo(keyword);
  // 整理成组件所需数据
  let showData = result.data.map((item: any) => {
    return {
      value: item.hosname, //展示医院的名字
      hoscode: item.hoscode, //存储医院的编码
    };
  });
  // 给组件提供展示的数据
  cb(showData);
};
// 点击某一个推荐项
const goDetail = (item: any) => {
  console.log(item);
  // 点击推荐项目进入医院详情页面，将来需要携带query参数（医院的编码）
  $router.push({
    path: "/hospital/register",
    query: { hoscode: item.hoscode },
  });
};
</script>

<script lang="ts">
export default {
  name: "Search",
};
</script>

<style scoped lang="scss">
// 深度选择器 css >>> less /deep/ sass ::v-deep
.search {
  width: 100%;
  //   height: 50px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  margin: 50px 0;
  ::v-deep(.el-input__wrapper) {
    width: 500px;
  }
}
</style>
